/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.handler;

import cn.ac.iie.di.commons.httpserver.framework.handler.HandlerI;
import cn.ac.iie.di.commons.httpserver.framework.util.ParseRequestStream;
import cn.ac.iie.di.subThread.AgentThread;
import cn.ac.iie.di.tools.FileTools;
import cn.ac.iie.di.tools.XmlTools;
import org.apache.log4j.Logger;
import org.eclipse.jetty.server.Request;
import cn.ac.iie.di.tools.StringTools;

/**
 *
 * @author zhangyun
 */
public class ExecuteHandler implements HandlerI {

    private static Logger logger = Logger.getLogger(ExecuteHandler.class);
    public static String reponsePath = ClassLoader.getSystemClassLoader().getResource("responseXML.xml").getFile();

    @Override
    public String execute(Request pRequest) throws Exception {
        String xml = ParseRequestStream.parseRequestStreamAsString(pRequest.getInputStream());
        String responseXml = FileTools.readFile(reponsePath);
        String subjobid = "";
        subjobid = XmlTools.getElValueFromStr(xml, "subjobid");
        String code = "0";
        String message = "nothing";
        responseXml = StringTools.replaceAll(responseXml, subjobid, code, message);
        logger.info("xml:" + xml);
        logger.info("responseXml:" + responseXml);
        //实际执行代理线程开启
        AgentThread agentThread = new AgentThread(xml, subjobid);
        Thread agThread = new Thread(agentThread);
        agThread.start();
        return responseXml;
    }
}
